package server.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import common.IClient;

import server.control.ServerControl;

import entity.*;

public class ServerFrame extends JFrame implements Runnable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3240834976980575030L;
	
	private ServerControl serverCtrl;
	
	//panel
	public ClientManagerPanel cmpanel;
	public FishManagerPanel fmpanel;
	public PoolPanel ppanel;
	public ControlPanel cpanel;
	
	public ServerFrame(ArrayList<IClient> clients, ServerControl serverCtrl) {
		
		this.serverCtrl = serverCtrl;
		
		//set size of window
		this.setPreferredSize(new Dimension(Constants.SERVER_WIDTH, Constants.SERVER_HEIGHT));
		this.setBackground(Color.WHITE);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setMaximumSize(new Dimension(Constants.SERVER_WIDTH, Constants.SERVER_HEIGHT));
		this.setMinimumSize(new Dimension(Constants.SERVER_WIDTH, Constants.SERVER_HEIGHT));
		
		initComponent();
		
		
		this.pack();
	}
	
	private void initComponent() {
		
		this.addWindowListener(new WindowAdapter() {

		    @Override
		    public void windowClosing(WindowEvent e) {
		    	try {
					Naming.unbind("server");
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		    }
		});
		
		//create panels
		cmpanel = new ClientManagerPanel(serverCtrl);
		fmpanel = new FishManagerPanel();
		ppanel = new PoolPanel(serverCtrl);
		cpanel = new ControlPanel(serverCtrl);
		
		cmpanel.setBorder(BorderFactory.createBevelBorder(0, Color.LIGHT_GRAY, Color.GRAY));
		fmpanel.setBorder(BorderFactory.createBevelBorder(0, Color.LIGHT_GRAY, Color.GRAY));
		ppanel.setBorder(BorderFactory.createBevelBorder(0, Color.LIGHT_GRAY, Color.GRAY));
		cpanel.setBorder(BorderFactory.createBevelBorder(0, Color.LIGHT_GRAY, Color.GRAY));
		
		//add panels in frame
		this.add(cmpanel);
		this.add(fmpanel);
		this.add(cpanel);
		this.add(ppanel);
		
		Thread t1 = new Thread(cmpanel);
		Thread t2 = new Thread(fmpanel);
		Thread t3 = new Thread(ppanel);
		t1.start();
		t2.start();
		t3.start();
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}

}